"""
访客用户模型
对应Node.js项目中的Visitor表，保持数据结构一致
"""

from sqlalchemy import Column, String, DateTime, Integer, Enum, Text, CHAR
from datetime import datetime
import enum

from .base import Base


class VisitorStatus(str, enum.Enum):
    """访客状态枚举"""
    ACTIVE = "active"
    BLOCKED = "blocked"


class Visitor(Base):
    """
    访客用户模型
    与Node.js项目中的visitors表保持一致
    """
    __tablename__ = "visitors"
    
    # UUID作为主键
    uuid = Column(CHAR(36), primary_key=True, nullable=False, comment="访客唯一标识符")
    
    # Google OAuth相关字段
    google_id = Column(String, unique=True, nullable=True, comment="Google账号ID")
    email = Column(String, nullable=True, comment="用户邮箱")
    display_name = Column(String, nullable=True, comment="用户显示名称")
    avatar = Column(String, nullable=True, comment="用户头像URL")
    
    # 登录相关
    last_login = Column(DateTime, nullable=True, comment="最后一次登录时间")
    login_count = Column(Integer, nullable=False, default=1, comment="累计登录次数")
    
    # 状态管理
    status = Column(String, nullable=False, default=VisitorStatus.ACTIVE.value, comment="访客状态")
    last_chat_type = Column(String, nullable=False, default="Girl", comment="最后一次聊天类型")
    
    # 密码相关（可选）
    password = Column(String, nullable=True, comment="用户密码（加密存储，可为空）")
    reset_password_token = Column(String, nullable=True, comment="重置密码token")
    reset_password_expires = Column(DateTime, nullable=True, comment="重置密码token过期时间")
    
    # Sequelize自动维护的时间戳
    created_at = Column(DateTime, nullable=False, default=datetime.utcnow, comment="创建时间")
    updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow, comment="更新时间")
    
    def __repr__(self):
        return f"<Visitor(uuid={self.uuid}, email={self.email}, status={self.status})>"
    
    def is_active(self) -> bool:
        """检查用户是否为活跃状态"""
        return self.status == VisitorStatus.ACTIVE.value
    
    def is_authenticated(self) -> bool:
        """检查用户是否已认证（有Google ID或密码）"""
        return self.google_id is not None or self.password is not None 